package com.sf.net;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

public class HttpUtil {

	private HttpUtil() {

	}

	public static String RequestGetURL(String path) {
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(path);
		try {
			HttpResponse rs = client.execute(get);
			if (rs.getStatusLine().getStatusCode() == 200) {
				HttpEntity httpEntity = rs.getEntity();
				return EntityUtils.toString(httpEntity, "utf-8");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public static String RequestPostURL(String uri) {
		HttpClient hc = new DefaultHttpClient();
		HttpPost hp = new HttpPost(uri);
		try {
			HttpResponse rs = hc.execute(hp);
			if (rs.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = rs.getEntity();
				return EntityUtils.toString(entity, "utf-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String downLoadContent(String path) {
		String json = null;
		InputStream is = null;
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		HttpURLConnection connection = null;
		try {
			URL url = new URL(path);

			connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
			connection.setConnectTimeout(30000);

			connection.setDoInput(true);
			connection.setDoOutput(true);
			connection.connect();
			int response_code = connection.getResponseCode();
			byte[] data = new byte[1024];
			int len = 0;
			if (response_code == 200) {
				is = connection.getInputStream();
				while ((len = is.read(data)) != -1) {
					os.write(data, 0, len);
				}
				json = os.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				connection.disconnect();
			}
		}
		return json;
	}

	public static void downPic(final String path, final ImageView imageView,
			final CallBack callBack) {
		final Handler handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				// String url = (String) msg.obj;
				Bitmap bitmap = (Bitmap) msg.obj;
				callBack.download(bitmap, path, imageView);
			}

		};

		new Thread(new Runnable() {

			@Override
			public void run() {

				HttpClient hc = new DefaultHttpClient();
				HttpGet hp = new HttpGet(path);
				try {
					HttpResponse rs = hc.execute(hp);
					if (rs.getStatusLine().getStatusCode() == 200) {
						byte[] result = EntityUtils.toByteArray(rs.getEntity());

						// String filename =
						// path.substring(path.lastIndexOf("/"),
						// path.length());
						// String uri = SdcardUtil.writeFiles(result,
						// filename);

						Bitmap bitmap = BitmapFactory.decodeByteArray(result,
								0, result.length);

						Message message = Message.obtain();
						// message.obj = uri;
						message.obj = bitmap;
						handler.sendMessage(message);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();

	}

	public interface CallBack {
		// public void download(Bitmap bitmap);
		// public void download(String url);
		// public void download(String uri, String path, ViewGroup group);

		public void download(Bitmap bitmap, String path, ImageView imageView);
	}

}
